﻿/*
 * Copyright 2012 SymSoftSolutions

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using GoogleOAuth2;
using System.Web.Script.Serialization;

namespace Test
{
    [TestFixture]
    public class ServiceAccountTest
    {
        [Test]
        public void getAccesstokenTest()
        {
            /* WARNING: 
               This test will fail unless you provide vaild email address and certificate file */
            string email = "xxxxxxxxxxx@developer.gserviceaccount.com";
            string certPath = @".\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12";
            string scope =  "https://www.googleapis.com/auth/fusiontables";

            string accessToken = GoogleOAuth2.ServiceAccount.getAccesstoken(email, certPath, scope);
            Assert.IsNotEmpty(accessToken);
        }
    }

    [TestFixture]    
    public class UtilityTest
    {
        [Test]
        public void getClaimsetTest()
        {
            string scope = "https://www.googleapis.com/auth/fusiontables";
            string email = "helloWorld@email.com";

            //Create claim json string
            string claimstr = ServiceAccountUtils.getClaimset(email, scope);
            
            // Deserialize for testing
            JavaScriptSerializer js = new JavaScriptSerializer();
            Dictionary<string, object> claim;
            claim = js.Deserialize<Dictionary<string, object>>(claimstr);

            // compare the results
            Assert.AreEqual(email, claim["iss"]);
            Assert.AreEqual(scope, claim["scope"]);
            Assert.AreEqual(ServiceAccount.GOOGLE_OAUTH2_ENDPOINT, claim["aud"]);
            Assert.Less((int)claim["iat"], (int)claim["exp"]);
        }

        [Test]
        public void getHeaderTest()
        {
            string actual = ServiceAccountUtils.getHeader("RS256", "JWT");
            string expected = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void jsonSerialzieStringTest()
        {
            Dictionary<string, object> obj = new Dictionary<string, object>();
            obj.Add("alg", "RS256");
            obj.Add("typ", "JWT");

            string actual = GoogleOAuth2.ServiceAccountUtils.jsonSerialize(obj);
            string expected = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void jsonSerialzieNumberTest()
        {
            Dictionary<string, object> obj = new Dictionary<string, object>();
            obj.Add("iat", 42);
            obj.Add("exp", 3.14);

            string actual = GoogleOAuth2.ServiceAccountUtils.jsonSerialize(obj);
            string expected = "{\"iat\":42,\"exp\":3.14}";
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void base64Test()
        {
            string input = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
            string actual = GoogleOAuth2.ServiceAccountUtils.base64Encoding(input);
            string expected = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9";

            Assert.AreEqual(expected, actual);
        }
    }
}
